package common;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.util.Hashtable;

import java.util.Map;

//import org.apache.commons.lang.builder.ToStringBuilder;
//import org.apache.commons.lang.builder.ToStringStyle;

public class UserSession
    implements HttpSessionBindingListener
{
    
    /**
     * 사용자 세션정보를 설정
     * 
     * @param map
     */
    public UserSession( Map map )
    {
        super();
        this.admin_id        = (String) map.get( "admin_id" );
        this.ent_id      = (String) map.get( "ent_id" );
        this.login_id       = (String) map.get( "login_id" );
        this.login_name       = (String) map.get( "login_name" );
        this.admin_level     = (String) map.get( "admin_level" );
        this.email      = (String) map.get( "email" );
        this.tel_no  = (String) map.get( "tel_no" );
        this.cell_no         = (String) map.get( "cell_no" );
        
        this.admin_logo_id         = (String) map.get( "admin_logo_id" );
              
    }

    /**
     * 사용자 세션정보를 설정
     * 
     * @param map
     */
public void setUserSession( Map map )
    {
        if ( map.containsKey( "admin_id" ) )
        {
            this.admin_id = (String) map.get( "admin_id" );
        }
        if ( map.containsKey( "ent_id" ) )
        {
            this.ent_id = (String) map.get( "ent_id" );
        }
        
        if ( map.containsKey( "login_id" ) )
        {
            this.login_id     = (String) map.get( "login_id" );           
        }
        if ( map.containsKey( "login_name" ) )
        {
            this.login_name = (String) map.get( "login_name" );
        }
        if ( map.containsKey( "admin_level" ) )
        {
            this.admin_level = (String) map.get( "admin_level" );
        }
        if ( map.containsKey( "email" ) )
        {
            this.email = (String) map.get( "email" );
        }
        if ( map.containsKey( "tel_no" ) )
        {
            this.tel_no = (String) map.get( "tel_no" );
        }
        if ( map.containsKey( "cell_no" ) )
        {
            this.cell_no = (String) map.get( "cell_no" );
        }
        
        if ( map.containsKey( "admin_logo_id" ) )
        {
            this.admin_logo_id = (String) map.get( "admin_logo_id" );
        }
       
    }
	public int getSessionCnt(){
		return LoginUsers.size();
	}
    /**
     * @see javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent)
     */
    public void valueBound( HttpSessionBindingEvent event )
    {
    	LoginUsers.put(event.getSession(), event.getName());
    	System.out.println(" "+ event.getName() +" 로그인");
    	System.out.println("현재 접속자 수 : " + LoginUsers.size());
    	event.getSession().getId();
//        ApplicationSessionManager.plusCurrentCount();
    }

    /**
     * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent)
     */
    public void valueUnbound( HttpSessionBindingEvent event )
    {
    		LoginUsers.remove(event.getSession());
    		System.out.println(" "+ event.getName() +" 로그아웃");
    		System.out.println("현재 접속자 수 : " + LoginUsers.size());
        // ApplicationSessionManager.minusCurrentCount();
    }
    
    public boolean isUsing(String userID){
    	return LoginUsers.containsValue(userID);
    }
    
    private String admin_id;
    private String ent_id;
    private String login_id;
    private String login_name;
    private String admin_level;
    private String email;
    private String tel_no;
    private String cell_no;
    private String admin_logo_id;
   
    private static Hashtable LoginUsers = new Hashtable(); 

    public String toString()
    {
//        ToStringBuilder ret = new ToStringBuilder( this, ToStringStyle.MULTI_LINE_STYLE );
    	StringBuffer ret= new StringBuffer();
        ret.append( " admin_id: " ).append( admin_id );
        ret.append( "\n ent_id: " ).append( ent_id );
        ret.append( "\n login_id: " ).append( login_id );
        ret.append( "\n login_name: " ).append( login_name );
        ret.append( "\n admin_level: " ).append( admin_level );
        ret.append( "\n email: " ).append( email );
        ret.append( "\n tel_no: " ).append( tel_no );
        ret.append( "\n cell_no: " ).append( cell_no );
        ret.append( "\n admin_logo_id: " ).append( admin_logo_id );
       
        return ret.toString();
    }

    public String getUser_id()
    {
        return admin_id;
    }
    public void setUser_id( String admin_id )
    {
        this.admin_id = admin_id;
    }
    
    public String getAdmin_level()
    {
        return admin_level;
    }
    public void setgetAdmin_level( String admin_level )
    {
        this.admin_level = admin_level;
    }
    
    public String getUser_name()
    {
        return login_name;
    }
    public void setUser_name( String login_name )
    {
        this.login_name = login_name;
    }
   
    public String getLogin_id()
    {
        return login_id;
    }
    public void setLogin_id( String login_id )
    {
        this.login_id = login_id;
    }
    
    public String getEnt_id()
    {
        return ent_id;
    }
    public void setEnt_id( String ent_id )
    {
        this.ent_id = ent_id;
    }

   

    public String getEmail_address()
    {
        return email;
    }

    public void setEmail_address( String email_address )
    {
        this.email = email_address;
    }

    public String getMobile()
    {
        return tel_no;
    }

    public void setMobile( String mobile )
    {
        this.tel_no = mobile;
    }

    public String getPhone()
    {
        return cell_no;
    }

    public void setPhone( String phone )
    {
        this.cell_no = phone;
    }
    
    
    public String getLogo()
    {
        return admin_logo_id;
    }

    public void setLogo( String admin_logo_id )
    {
        this.admin_logo_id = admin_logo_id;
    }

   
}
